package com.ysw.springbootproject.service.impl;

import com.ysw.springbootproject.dao.ArticleDao;
import com.ysw.springbootproject.entity.Article;
import com.ysw.springbootproject.service.ArticleService;
import com.ysw.springbootproject.utils.PageResult;
import com.ysw.springbootproject.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 月上微
 * @date 2025/1/9
 * @since 10:43
 */
@Service("articleService")
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    /**
     * 分页查询文章
     *
     * @param pageUtil PageUtil
     * @return PageResult
     */
    @Override
    public PageResult getArticlePage(PageUtil pageUtil) {
        List<Article> articleList = articleDao.findArticles(pageUtil);
        int total = articleDao.getTotalArticles(pageUtil);
        PageResult pageResult = new PageResult(articleList, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    /**
     * 根据id查询文章
     *
     * @param id id
     * @return Article
     */
    @Override
    public Article queryObject(Integer id) {
        Article article = articleDao.getArticleById(id);
        if (article != null) {
            return article;
        }
        return null;
    }

    /**
     * 根据map查询文章
     *
     * @param map map
     * @return List
     */
    @Override
    public List<Article> queryList(Map<String, Object> map) {
        List<Article> articleList = articleDao.findArticles(map);
        return articleList;
    }

    /**
     * 根据map查询文章总数
     *
     * @param map map
     * @return int
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return articleDao.getTotalArticles(map);
    }

    /**
     * 保存文章
     *
     * @param article Article
     * @return int
     */
    @Override
    public int save(Article article) {
        return articleDao.insertArticle(article);
    }

    /**
     * 更新文章
     *
     * @param article Article
     * @return int
     */
    @Override
    public int update(Article article) {
        article.setUpdateTime(new Date());
        return articleDao.updateArticle(article);
    }

    /**
     * 删除文章
     *
     * @param id id
     * @return int
     */
    @Override
    public int delete(Integer id) {
        return articleDao.deleteArticle(id);
    }

    /**
     * 批量删除文章
     *
     * @param ids ids
     * @return int
     */
    @Override
    public int deleteBatch(Integer[] ids) {
        return articleDao.deleteBatch(ids);
    }
}
